package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.user.mapper.UserPaidAlbumMapper;
import com.atguigu.tingshu.user.strategy.PurchaseStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * ClassName: AlbumPurchaseStrategy
 * Package: com.atguigu.tingshu.user.strategy.impl
 * Description:
 *
 * @Author:
 * @Create: 2025/8/9 - 23:38
 * @Version: v1.0
 */

@Service("1001")
@Slf4j
public class AlbumPurchaseStrategy implements PurchaseStrategy {

    @Resource
    private UserPaidAlbumMapper userPaidAlbumMapper;

    @Override
    public void purchase(UserPaidRecordVo userPaidRecordVo) {
        LambdaQueryWrapper<UserPaidAlbum> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPaidAlbum::getUserId, userPaidRecordVo.getUserId());
        queryWrapper.eq(UserPaidAlbum::getAlbumId, userPaidRecordVo.getItemIdList().get(0));
        UserPaidAlbum userPaidAlbum = userPaidAlbumMapper.selectOne(queryWrapper);
        if (userPaidAlbum != null) {
            log.info("当前用户：{}，已经购买过该专辑：{}", userPaidRecordVo.getUserId(), userPaidRecordVo.getItemIdList().get(0));
            return;
        }
        //构造数据
        userPaidAlbum = new UserPaidAlbum();
        userPaidAlbum.setUserId(userPaidRecordVo.getUserId());
        userPaidAlbum.setAlbumId(userPaidRecordVo.getItemIdList().get(0));
        userPaidAlbum.setOrderNo(userPaidRecordVo.getOrderNo());
        userPaidAlbumMapper.insert(userPaidAlbum);
    }
}
